﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HyEduOnline.Services;
using System.Web.Security;
using HyEduOnline.Models;
using System.Net;
using HyEduOnline.Server.Common;
namespace HyEduOnline.Services
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class UserAuthorizeTea : System.Web.Mvc.AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException("HttpContext");
            }

            if (frontCurrentTea.GetUserID() < 1)
            {
                //未认证

                //从Cookie中取 
                if (httpContext.Request.Cookies.AllKeys.Contains("User"))
                {
                    string name = httpContext.Request.Cookies["User"]["LoginName"];
                    string pass = httpContext.Request.Cookies["User"]["LoginPassWord"];
                    string passWord = encryption.XOR(pass);//解密密码
                    string userType = httpContext.Request.Cookies["User"]["UserType"];
                    int type = int.Parse(userType);
                    if (!String.IsNullOrEmpty(name) && !String.IsNullOrEmpty(passWord) && type == 1 )//教师
                    {
                        FormsAuthentication.SetAuthCookie(name, false);
                        FormsAuthentication.SetAuthCookie(passWord, false);

                        passWord = Utitil.MD5(passWord);//对密码加密
                        if (LoginValidate.ValidateTeacher(name, passWord))
                        {   //验证登陆 
                            
                            if (!HttpContext.Current.Response.IsRequestBeingRedirected)
                            {
                                //do the redirect

                                HttpContext.Current.Response.Redirect("~/Teacher/Index");
                                HttpContext.Current.Response.End();
                            }
                        }

                        return true;
                    }
                    else
                    {
                        if (!HttpContext.Current.Response.IsRequestBeingRedirected)
                        {
                            //do the redirect

                            HttpContext.Current.Response.Redirect("~/Login/CommonUser");
                            HttpContext.Current.Response.End();
                        }
                        
                        return false;
                    }
                }
                else
                {
                    
                    if (!HttpContext.Current.Response.IsRequestBeingRedirected)
                    {
                        //do the redirect
                        HttpContext.Current.Response.Redirect("~/Login/CommonUser");
                        HttpContext.Current.Response.End();
                    }
                    return false;
                }
            }
            return true;
        }
    }
}